package leetcode11_20;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author shhjiang_001
 * @create 2020-03-09 20:08
 */
public class Solution15 {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(nums);
        for (int i =0;i < nums.length-2; i++){
            if (i == 0 || (i>0 && nums[i]!=nums[i-1] )){
                int le = i+1, ri = nums.length-1;
                int m = -nums[i];
                while (le < ri){
                    if (nums[ri]+nums[le] == m ){
                        list.add(Arrays.asList(nums[i],nums[le],nums[ri]));
                        while (le < ri && nums[le]== nums[le+1]) le++;
                        while (le < ri && nums[ri]== nums[ri-1]) ri--;
                        le++;
                        ri--;
                    }else if (nums[ri]+nums[le] < m) {
                        while (le < ri && nums[le]== nums[le+1]) le++;
                        le++;
                    }else {
                        while (le < ri && nums[ri]== nums[ri-1]) ri--;
                        ri--;
                    }

                }
            }

        }
        return list;

    }

    @Test
    public void test(){
        Solution15 solution15 = new Solution15();
        List<List<Integer>> lists = solution15.threeSum(new int[]{0, 0, 0});


    }
}
